package com.kun.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalInfo;
import com.kun.rental.mapper.CarrentalInfoMapper;
import com.kun.rental.service.ICarrentalInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.rental.service.IOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ikun
 * @since 2024-07-04
 */
@Service
public class CarrentalInfoServiceImpl extends ServiceImpl<CarrentalInfoMapper, CarrentalInfo> implements ICarrentalInfoService {


    @Autowired
    private CarrentalInfoMapper carrentalInfoMapper;

    @Autowired
    private IOssService ossService;

    /**
     * 分页查询
     * @param page
     * @param carrentalInfo
     * @return
     */
    @Override
    public Page<CarrentalInfo> searchByPage(Page<CarrentalInfo> page, CarrentalInfo carrentalInfo) {

        return carrentalInfoMapper.searchByPage(page, carrentalInfo);
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public boolean delete(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        if (!list.isEmpty()){
            //同时删除图片
            list.forEach(id->{
                CarrentalInfo carrentalInfo = carrentalInfoMapper.selectById(id);
                if (carrentalInfo!=null){
                    String pic = carrentalInfo.getPic();
                    if (pic!=null){
                        //删除OSS中的图片
                        ossService.delete(pic);
                    }
                }
            });
            return carrentalInfoMapper.deleteBatchIds(list)>0;
        }
        return false;
    }


    /**
     * 根据车牌号查询车辆信息
     * @param num
     * @return
     */
    @Override
    public CarrentalInfo selectByCarrentalNum(String num) {
        QueryWrapper<CarrentalInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("num",num);
        return carrentalInfoMapper.selectOne(queryWrapper);
    }


    /**
     * 查询需要保养的车辆
     * @return
     */
    @Override
    public List<CarrentalInfo> toBeMaintain() {
        return carrentalInfoMapper.toBeMaintain();
    }


}
